﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2011
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/
package evideo.controllers.display 
{
    import evideo.display.application.VideoProtector;

    import graphics.display.Background;

    import lunas.Button;

    import vegas.controllers.AbstractController;

    import flash.display.Stage;
    import flash.display.StageDisplayState;
    import flash.events.Event;
    import flash.media.Video;

    /**
     * Invoked when the stage fullscreen mode is changed.
     */
    public class FullScreen extends AbstractController 
    {
        /**
         * Creates a new FullScreen instance.
         */
        public function FullScreen()
        {
            super();
        }
        
        /**
         * The fullscreen button reference.
         */
        public var button:Button ;
        
        /**
         * The video protector reference.
         */
        public var protector:VideoProtector ;
        
        /**
         * The remote reference.
         */
        public var remote:Background ;
        
        /**
         * The stage reference.
         */
        public var stage:Stage ;
        
        /**
         * The video reference.
         */
        public var video:Video ;
        
        /**
         * Handles the event.
         */
        public override function handleEvent( e:Event ):void
        {
            if ( stage )
            {
                logger.debug(this + " handleEvent.") ;
                var isFull:Boolean = stage && stage.displayState == StageDisplayState.FULL_SCREEN ;
                if ( button != null )
                { 
                    button.setSelected( isFull, true ) ;
                }
                if ( protector )
                {
                    if ( isFull )
                    {
                        stage.addChild( protector ) ;
                        protector.video  = video ;
                        protector.remote = remote ;
                    }
                    else if ( stage.contains( protector ) )
                    {
                        stage.removeChild( protector ) ;
                        protector.dispose() ;
                        if ( remote )
                        {
                            remote.visible = true ;
                        }
                    }
                }
            }
            else
            {
                logger.warn(this + " handleEvent failed, the stage reference not must be null.") ;
            }
        }
    }
}
